package com.common.web.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.common.web.service.JwtTokenAuthHandlerInterceptor;
import com.common.web.util.WebConstant;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer{

	@Autowired
	private JwtTokenAuthHandlerInterceptor jwtTokenAuthHandlerInterceptor;
	
	public WebMvcConfig() {
		log.info("初始化WebMvcConfig........"+this.getClass().getSimpleName());
	}
		
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		WebMvcConfigurer.super.addResourceHandlers(registry);
//		registry.addResourceHandler("/templates/**.js").addResourceLocations("classpath:/templates/");
//        registry.addResourceHandler("/templates/**.css").addResourceLocations("classpath:/templates/");
//        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
//        registry.addResourceHandler("swagger-ui.html")
//                .addResourceLocations("classpath:/META-INF/resources/");
//        registry.addResourceHandler("/webjars/**")
//                .addResourceLocations("classpath:/META-INF/resources/webjars/");
	}
	
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//                .allowedOrigins("*")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }
    /**
     * 添加拦截器
    */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(jwtTokenAuthHandlerInterceptor)
			.addPathPatterns("/*","/*/*","/*/*/*","/*/*/*/*")
			.excludePathPatterns(WebConstant.webResourceList)
			.excludePathPatterns(WebConstant.whiteRequestList);
	}
}